//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.doc.server.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.farm.authority.FarmAuthorityService;
import com.farm.core.sql.result.DataResult;
import com.farm.core.sql.result.ResultsHandle;
import com.farm.doc.dao.FarmDocDaoInter;
import com.farm.doc.domain.Doc;
import com.farm.doc.domain.ex.DocBrief;
import com.farm.doc.domain.ex.DocEntire;
import com.farm.doc.domain.ex.FileIndexInfo;
import com.farm.doc.domain.ex.FqaBrief;
import com.farm.doc.server.FarmDocIndexInter;
import com.farm.doc.server.FarmDocTypeInter;
import com.farm.doc.server.FarmDocgroupManagerInter;
import com.farm.doc.server.utils.IndexUtilsDomain;
import com.farm.doc.util.HtmlUtils;
import com.farm.doc.util.LuceneDocUtil;
import com.farm.lucene.FarmLuceneFace;
import com.farm.lucene.common.ScoreDocFilterInter;
import com.farm.lucene.server.DocIndexInter;
import com.farm.lucene.server.DocQueryImpl;
import com.farm.lucene.server.DocQueryInter;
import com.farm.util.web.FarmFormatUnits;
import com.farm.util.web.WebHotCase;

@Service
public class FarmDocIndexManagerImpl implements FarmDocIndexInter {
	private static final Logger log = Logger.getLogger(FarmDocIndexManagerImpl.class);
	
	@Resource
	private FarmDocDaoInter farmDocDao;
	@Resource
	private FarmDocTypeInter farmDocTypeManagerImpl;
	@Resource
	private FarmDocgroupManagerInter farmDocgroupManagerImpl;

	public FarmDocIndexManagerImpl() {
	}

	@Transactional
	public List<DocBrief> getRelationDocs(String docid, int num) {
		Doc doc = this.farmDocDao.getEntity(docid);
		DocQueryInter query = DocQueryImpl.getInstance(FarmLuceneFace.inctance().getIndexPathFile("docindex"));
		String iql = null;
		if (iql == null) {
			iql = "WHERE(TITLE,TEXT=" + HtmlUtils.HtmlRemoveTag(doc.getTitle().trim()).replaceAll(":", "") + ")";
		}

		try {
			final Set<String> typesset = new HashSet(this.farmDocTypeManagerImpl.getPubTypeIds());
			DataResult result = query.queryByMultiIndex(iql, 1, num, new ScoreDocFilterInter() {
				public boolean doScan(Document document) {
					return typesset.contains(document.get("TYPEID"));
				}
			}).getDataResult();
			result.runHandle(new ResultsHandle() {
				public void handle(Map<String, Object> row) {
					row.put("DOCID", row.get("ID"));
				}
			});
			return result.getObjectList(DocBrief.class);
		} catch (Exception var8) {
			log.error(var8.toString());
			return new ArrayList();
		}
	}

	@Transactional
	public DataResult search(String word, final String userid, Integer pagenum) throws Exception {
		DocQueryInter query = DocQueryImpl.getInstance(FarmLuceneFace.inctance().getIndexPathFile("docindex"));
		String iql = null;
		word = HtmlUtils.HtmlRemoveTag(word.trim());
		if (word.indexOf("TYPE:") >= 0 && iql == null) {
			word = word.substring(word.indexOf("TYPE:") + 5).replaceAll(":", "");
			iql = "WHERE(TYPENAME=" + word + ")";
		}

		if (word.indexOf("TAG:") >= 0 && iql == null) {
			word = word.substring(word.indexOf("TYPE:") + 5).replaceAll(":", "");
			iql = "WHERE(TAGKEY=" + word + ")";
		}

		if (word.indexOf("AUTHOR:") >= 0 && iql == null) {
			word = word.substring(word.indexOf("AUTHOR:") + 7).replaceAll(":", "");
			iql = "WHERE(AUTHOR=" + word + ")";
		}

		if (word.indexOf("TITLE:") >= 0 && iql == null) {
			word = word.substring(word.indexOf("TITLE:") + 6).replaceAll(":", "");
			iql = "WHERE(TITLE=" + word + ")";
		}

		if (iql == null) {
			iql = "WHERE(TITLE,TEXT,TAGKEY,TYPENAME,AUTHOR=" + word.replaceAll(":", "") + ")";
		}

		if (pagenum == null) {
			pagenum = Integer.valueOf(1);
		}

		WebHotCase.putCase(word);
		new ArrayList();
		List<String> types = this.farmDocTypeManagerImpl.getPubTypeIds();
		if (userid != null && !userid.isEmpty()) {
			types.addAll(this.farmDocTypeManagerImpl
					.getUserReadTypeIds(FarmAuthorityService.getInstance().getUserById(userid)));
		}

		List<String> groups = this.farmDocgroupManagerImpl.getGroupIdsByUser(userid);
		final Set<String> typesset = new HashSet(types);
		final Set<String> groupsset = new HashSet(groups);
		DataResult result = query.queryByMultiIndex(iql, pagenum.intValue(), 10, new ScoreDocFilterInter() {
			public boolean doScan(Document document) {
				if (document.get("READPOP") == null) {
					return typesset.contains(document.get("TYPEID"));
				} else {
					String var2;
					switch ((var2 = document.get("READPOP")).hashCode()) {
					case 48:
						if (var2.equals("0")) {
							if (userid != null && userid.equals(document.get("USERID"))) {
								return true;
							}

							return false;
						}
						break;
					case 49:
						if (var2.equals("1")) {
							if (typesset.contains(document.get("TYPEID"))) {
								return true;
							}

							return false;
						}
						break;
					case 50:
						if (var2.equals("2")) {
							if (groupsset.contains(document.get("GROUPID"))) {
								return true;
							}

							return false;
						}
					}

					return false;
				}
			}
		}).getDataResult();
		Iterator var13 = result.getResultList().iterator();

		while (true) {
			while (var13.hasNext()) {
				Map<String, Object> node = (Map) var13.next();
				String tags = node.get("TAGKEY").toString();
				String text = node.get("TEXT").toString();
				node.put("DOCDESCRIBE", text.length() > 256 ? text.substring(0, 256) : text);
				if (tags != null && tags.trim().length() > 0) {
					List<String> tags1 = FarmFormatUnits.parseTags(tags.trim());
					node.put("TAGKEY", tags1);
				} else {
					node.put("TAGKEY", new ArrayList());
				}
			}

			return result;
		}
	}

	public DataResult search(String word, List<String> typeIds, List<String> groupIds, String userid, Integer pagenum)
			throws Exception {
		List<File> files = new ArrayList();
		files.add(FarmLuceneFace.inctance().getIndexPathFile("docindex"));
		DocQueryInter query = DocQueryImpl.getInstance(files);
		String iql = null;
		word = HtmlUtils.HtmlRemoveTag(word.trim());
		if (word.indexOf("TYPE:") >= 0 && iql == null) {
			word = word.substring(word.indexOf("TYPE:") + 5).replaceAll(":", "");
			iql = "WHERE(TYPENAME=" + word + ")";
		}

		if (word.indexOf("TAG:") >= 0 && iql == null) {
			word = word.substring(word.indexOf("TYPE:") + 5).replaceAll(":", "");
			iql = "WHERE(TAGKEY=" + word + ")";
		}

		if (word.indexOf("AUTHOR:") >= 0 && iql == null) {
			word = word.substring(word.indexOf("AUTHOR:") + 7).replaceAll(":", "");
			iql = "WHERE(AUTHOR=" + word + ")";
		}

		if (word.indexOf("TITLE:") >= 0 && iql == null) {
			word = word.substring(word.indexOf("TITLE:") + 6).replaceAll(":", "");
			iql = "WHERE(TITLE=" + word + ")";
		}

		if (iql == null) {
			iql = "WHERE(TITLE,TEXT,TAGKEY,TYPENAME,AUTHOR=" + word.replaceAll(":", "") + ")";
		}

		if (pagenum == null) {
			pagenum = Integer.valueOf(1);
		}

		WebHotCase.putCase(word);
		final Set<String> typesset = new HashSet(typeIds);
		final Set<String> groupsset = new HashSet(groupIds);
		DataResult result = query.queryByMultiIndex(iql, pagenum.intValue(), 10, new ScoreDocFilterInter() {
			public boolean doScan(Document document) {
				if (typesset.contains(document.get("TYPEID"))) {
					return true;
				} else {
					return groupsset.contains(document.get("GROUPID"));
				}
			}
		}).getDataResult();
		Iterator var13 = result.getResultList().iterator();

		while (true) {
			while (var13.hasNext()) {
				Map<String, Object> node = (Map) var13.next();
				String tags = node.get("TAGKEY").toString();
				String text = node.get("TEXT").toString();
				node.put("DOCDESCRIBE", text.length() > 256 ? text.substring(0, 256) : text);
				if (tags != null && tags.trim().length() > 0) {
					List<String> tags1 = FarmFormatUnits.parseTags(tags.trim());
					node.put("TAGKEY", tags1);
				} else {
					node.put("TAGKEY", new ArrayList());
				}
			}

			return result;
		}
	}

	@Transactional
	public void addLuceneIndex(DocEntire entity) {
		DocIndexInter index = null;
		if (entity.getCurrenttypes() == null && entity.getType() != null && entity.getType().getId() != null) {
			entity.setCurrenttypes(this.farmDocTypeManagerImpl.getTypeAllParent(entity.getType().getId()));
		}

		try {
			FarmLuceneFace luceneImpl = FarmLuceneFace.inctance();
			index = luceneImpl.getDocIndex(luceneImpl.getIndexPathFile("docindex"));
			index.indexDoc(LuceneDocUtil.getDocMap(entity));
		} catch (Exception var11) {
			throw new RuntimeException(var11);
		} finally {
			try {
				if (index != null) {
					index.close();
				}
			} catch (Exception var10) {
				log.error("lucene error:" + var10);
			}

		}

	}

	@Transactional
	public void addLuceneIndex(FqaBrief fqabrief) {
		DocIndexInter index = null;
		if (fqabrief.getTypes() == null && fqabrief.getType() != null && fqabrief.getType().getId() != null) {
			fqabrief.setTypes(this.farmDocTypeManagerImpl.getTypeAllParent(fqabrief.getType().getId()));
		}

		try {
			FarmLuceneFace luceneImpl = FarmLuceneFace.inctance();
			index = luceneImpl.getDocIndex(luceneImpl.getIndexPathFile("docindex"));
			index.indexDoc(LuceneDocUtil.getDocMap(fqabrief));
		} catch (Exception var11) {
			throw new RuntimeException(var11);
		} finally {
			try {
				if (index != null) {
					index.close();
				}
			} catch (Exception var10) {
				log.error("lucene error:" + var10);
			}

		}

	}

	@Transactional
	public void addLuceneIndex(String fileid, String name, String text, DocEntire entity) {
		DocIndexInter index = null;

		try {
			FarmLuceneFace luceneImpl = FarmLuceneFace.inctance();
			index = luceneImpl.getDocIndex(luceneImpl.getIndexPathFile("docindex"));
			index.indexDoc(LuceneDocUtil.convertFileMap(LuceneDocUtil.getDocMap(entity), fileid, name, text));
		} catch (Exception var14) {
			throw new RuntimeException(var14);
		} finally {
			try {
				if (index != null) {
					index.close();
				}
			} catch (Exception var13) {
				log.error("lucene error:" + var13);
			}

		}

	}

	@Transactional
	public void delLuceneIndex(DocEntire doc) {
		this.delLuceneIndex(doc.getDoc().getId());
	}

	@Transactional
	public void delLuceneIndex(String indexId) {
		DocIndexInter index = null;

		try {
			FarmLuceneFace luceneImpl = FarmLuceneFace.inctance();
			index = luceneImpl.getDocIndex(luceneImpl.getIndexPathFile("docindex"));
			index.deleteFhysicsIndex(indexId);
		} catch (Exception var11) {
			throw new RuntimeException(var11 + "删除索引");
		} finally {
			try {
				if (index != null) {
					index.close();
				}
			} catch (Exception var10) {
				var10.printStackTrace();
				log.error("lucene error:" + var10);
			}

		}

	}

	@Transactional
	public void reLuceneIndex(DocEntire olddoc, DocEntire newdoc) {
		this.delLuceneIndex(olddoc);
		this.addLuceneIndex(newdoc);
	}

	public void delLuceneIndexs(List<DocEntire> docs, IndexUtilsDomain domain) {
		DocIndexInter index = null;
		if (docs.size() > 0) {
			try {
				int n = 0;
				Iterator var6 = docs.iterator();

				while (var6.hasNext()) {
					DocEntire doc = (DocEntire) var6.next();

					try {
						domain.setProcessNum(n++);
						log.info("删除索引：" + n + "/" + docs.size());
						FarmLuceneFace luceneImpl = FarmLuceneFace.inctance();
						if (index == null) {
							index = luceneImpl.getDocIndex(luceneImpl.getIndexPathFile("docindex"));
						}

						index.deleteFhysicsIndex(doc.getDoc().getId());
					} catch (Exception var15) {
						throw new RuntimeException(var15 + "删除索引");
					}
				}
			} finally {
				try {
					index.close();
				} catch (Exception var14) {
					throw new RuntimeException(var14 + "删除索引");
				}
			}

		}
	}

	public void delLuceneIndexsByFile(List<FileIndexInfo> fileindexs, IndexUtilsDomain domain) {
		DocIndexInter index = null;
		if (fileindexs.size() > 0) {
			try {
				int n = 0;
				Iterator var6 = fileindexs.iterator();

				while (var6.hasNext()) {
					FileIndexInfo fileindex = (FileIndexInfo) var6.next();

					try {
						domain.setProcessNum(n++);
						log.info("删除索引：" + n + "/" + fileindexs.size());
						FarmLuceneFace luceneImpl = FarmLuceneFace.inctance();
						if (index == null) {
							index = luceneImpl.getDocIndex(luceneImpl.getIndexPathFile("docindex"));
						}

						index.deleteFhysicsIndex(fileindex.getFileid());
					} catch (Exception var15) {
						throw new RuntimeException(var15 + "删除索引");
					}
				}
			} finally {
				try {
					index.close();
				} catch (Exception var14) {
					throw new RuntimeException(var14 + "删除索引");
				}
			}

		}
	}

	public void delLuceneIndexsByFqa(List<FqaBrief> docs) {
		this.delLuceneIndexsByFqa(docs, (IndexUtilsDomain) null);
	}

	public void delLuceneIndexsByFqa(List<FqaBrief> docs, IndexUtilsDomain domain) {
		DocIndexInter index = null;
		if (docs.size() > 0) {
			try {
				int n = 0;
				Iterator var6 = docs.iterator();

				while (var6.hasNext()) {
					FqaBrief doc = (FqaBrief) var6.next();

					try {
						if (domain != null) {
							domain.setProcessNum(n++);
						}

						log.info("删除索引：" + n + "/" + docs.size());
						FarmLuceneFace luceneImpl = FarmLuceneFace.inctance();
						if (index == null) {
							index = luceneImpl.getDocIndex(luceneImpl.getIndexPathFile("docindex"));
						}

						index.deleteFhysicsIndex(doc.getQuestionid());
					} catch (Exception var15) {
						throw new RuntimeException(var15 + "删除索引");
					}
				}
			} finally {
				try {
					index.close();
				} catch (Exception var14) {
					throw new RuntimeException(var14 + "删除索引");
				}
			}

		}
	}

	public void addLuceneIndexsByFqa(List<FqaBrief> fqas) {
		this.addLuceneIndexsByFqa(fqas, (IndexUtilsDomain) null);
	}

	public void addLuceneIndexsByFqa(List<FqaBrief> fqas, IndexUtilsDomain domain) {
		DocIndexInter index = null;

		try {
			FarmLuceneFace luceneImpl = FarmLuceneFace.inctance();
			index = luceneImpl.getDocIndex(luceneImpl.getIndexPathFile("docindex"));
			int n = 0;
			Iterator var7 = fqas.iterator();

			while (var7.hasNext()) {
				FqaBrief doc = (FqaBrief) var7.next();

				try {
					if (domain != null) {
						domain.setProcessNum(n++);
					}

					if (doc.getTypes() == null && doc.getType() != null && doc.getType().getId() != null) {
						doc.setTypes(this.farmDocTypeManagerImpl.getTypeAllParent(doc.getType().getId()));
					}

					index.indexDoc(LuceneDocUtil.getDocMap(doc));
				} catch (Exception var17) {
					throw new RuntimeException(var17 + "添加索引" + doc.getTitle());
				}
			}
		} catch (Exception var18) {
			var18.printStackTrace();
			throw new RuntimeException(var18);
		} finally {
			try {
				if (index != null) {
					index.close();
				}
			} catch (Exception var16) {
				log.error("lucene error:" + var16);
			}

		}

	}

	public void addLuceneIndexs(List<DocEntire> docs, IndexUtilsDomain domain) {
		DocIndexInter index = null;

		try {
			FarmLuceneFace luceneImpl = FarmLuceneFace.inctance();
			index = luceneImpl.getDocIndex(luceneImpl.getIndexPathFile("docindex"));
			int n = 0;
			Iterator var7 = docs.iterator();

			while (var7.hasNext()) {
				DocEntire doc = (DocEntire) var7.next();

				try {
					domain.setProcessNum(n++);
					if (doc.getCurrenttypes() == null && doc.getType() != null && doc.getType().getId() != null) {
						doc.setCurrenttypes(this.farmDocTypeManagerImpl.getTypeAllParent(doc.getType().getId()));
					}

					index.indexDoc(LuceneDocUtil.getDocMap(doc));
				} catch (Exception var17) {
					throw new RuntimeException(var17 + "添加索引");
				}
			}
		} catch (Exception var18) {
			throw new RuntimeException(var18);
		} finally {
			try {
				if (index != null) {
					index.close();
				}
			} catch (Exception var16) {
				log.error("lucene error:" + var16);
			}

		}

	}

	public void addLuceneIndexsByFile(List<FileIndexInfo> fileindexs, IndexUtilsDomain domain) {
		DocIndexInter index = null;

		try {
			FarmLuceneFace luceneImpl = FarmLuceneFace.inctance();
			index = luceneImpl.getDocIndex(luceneImpl.getIndexPathFile("docindex"));
			int n = 0;
			Iterator var7 = fileindexs.iterator();

			while (var7.hasNext()) {
				FileIndexInfo fileindex = (FileIndexInfo) var7.next();

				try {
					domain.setProcessNum(n++);
					if (fileindex.getDocEntity().getCurrenttypes() == null && fileindex.getDocEntity().getType() != null
							&& fileindex.getDocEntity().getType().getId() != null) {
						fileindex.getDocEntity().setCurrenttypes(this.farmDocTypeManagerImpl
								.getTypeAllParent(fileindex.getDocEntity().getType().getId()));
					}

					index.indexDoc(LuceneDocUtil.convertFileMap(LuceneDocUtil.getDocMap(fileindex.getDocEntity()),
							fileindex.getFileid(), fileindex.getName(), fileindex.getText()));
				} catch (Exception var17) {
					throw new RuntimeException(var17 + "添加索引");
				}
			}
		} catch (Exception var18) {
			throw new RuntimeException(var18);
		} finally {
			try {
				if (index != null) {
					index.close();
				}
			} catch (Exception var16) {
				log.error("lucene error:" + var16);
			}

		}

	}
}
